package com.example.cms.web.Controller;

import com.example.common.Utils.PageUtil;
import com.example.cms.Service.IConfigService;
import com.example.common.web.exception.CustomerException;
import com.example.common.web.response.Result;
import com.example.common.web.response.ResultCode;
import com.example.logging.anno.LoggingAccess;
import com.example.user.bean.Config;
import com.github.pagehelper.PageInfo;
import com.google.protobuf.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/10/31-10-31-11:11
 * @Description：com.example.cms.web.Controller
 */

@Api(tags = "配置模块")    //模块中文
@RestController
@RequestMapping("/config")
public class ConfigController {

    @Autowired        //从容器中取出实现类
    private IConfigService service;

    @LoggingAccess
    @ApiOperation(value = "查询所有已开启的配置",notes = "未分页")
    @GetMapping("/")
    public Result selectConfigStatusWithOn(){
//        throw new CustomerException("自定义错误");

        //throw new CustomerException(ResultCode.DATA_NONE);
        return Result.success(service.selectAllConfig());
    }

    @ApiOperation(value = "添加配置",notes = "id和status不需要传值")
    @PostMapping("/")
    public Result addConfig(@RequestBody @Valid Config config){
       //  System.out.println(config);
        config.setConfigStatus(0);     //默认配置状态为未开启状态
        service.addConfig(config);
        return Result.success("添加成功");
    }
    /*
    //修改配置
    void updateConfig(Config config);
    //修改配置状态
    void updateStatusById(Integer id);
    //删除配置
    void deleteConfigById(Integer id);
    //分页查找所有配置
     */
    @PutMapping("/updateConfig")
    public Result updateConfig(@RequestBody Config config){
        service.updateConfig(config);
        return Result.success("修改配置信息成功");
    }


    @PutMapping("/updateStatus")
    public Result updateConfigStatus(@RequestParam("id") Integer id){

        service.updateStatusById(id);
        return Result.success("修改状态成功");
    }
    @ApiOperation(value = "删除配置", notes = "物理删除")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "编号",dataType = "int",paramType = "query", required = true)})
    @DeleteMapping("/deleteConfig")
    public Result deleteConfigById(@RequestParam Integer id){
        service.deleteConfigById(id);
        return Result.success("成功删除配置信息");
    }
    @ApiOperation(value = "分页查询所有配置",notes = "提供分页参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pages", value = "当前页", dataType = "int", paramType = "query", defaultValue = "0"),
            @ApiImplicitParam(name = "nums", value = "每页条数", dataType = "int", paramType = "query", defaultValue = "5")
    })
    @GetMapping("/pageSelectConfig")
    public Result PageOfConfig(@RequestParam("pages") int pages, @RequestParam("nums") int nums ) throws ServiceException {
        PageInfo<Config> configPageInfo = service.selectConfigPages(pages, nums);
        return Result.success(configPageInfo);
    }

    @ApiOperation(value = "分页查询配置简洁版",notes = "自制版")
    @PostMapping("/pageSelectConfigTwo")
    public Result selectByPageTwo(@RequestBody PageUtil pageUtil){
        PageUtil pageUtil1 = service.selectListByPage(pageUtil);
        return Result.success(pageUtil1);
    }
}
